/**
 * Copyright © 2019 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package com.siping.并发.黑马并发实战.自定义连接池;

import org.apache.commons.lang.math.RandomUtils;

import java.util.Random;

/**
 * @author YangXu
 * @date 2022/3/3
 * @description:
 */
public class ObjectPoolTest {
    public static void main(String[] args) {
        ObjectPool<Integer> pool = new ObjectPool<>(2, new PoolObjectFactory<Integer>() {
            @Override
            public PoolObject<Integer> makeObject() {
                return new PoolObject<>(RandomUtils.nextInt(100));
            }
        });

        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                Integer obj = pool.borrowObject();
                try {
                    Thread.sleep(new Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                pool.returnObject(obj);
            }).start();
        }
    }
}
